namespace Digitalmes.Application.Mes;

/// <summary>
/// 设备警报项定义表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class EquipAlarmItemService : IDynamicWebApiService
{
    private readonly SugarRepository<EquipAlarmItem> _thisRepository;
    private readonly IEquipAlarmItemQueryService _queryService;

    public EquipAlarmItemService(SugarRepository<EquipAlarmItem> thisRepository, IEquipAlarmItemQueryService queryService)
    {
        _thisRepository = thisRepository;
        _queryService = queryService;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<EquipAlarmItemDto>> GetPagesAsync(EquipAlarmItemPageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrEmpty(param.EquipName), s => s.EquipName == param.EquipName)
            .OrderBy(m => m.Pos)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<EquipAlarmItemDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<EquipAlarmItemDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<EquipAlarmItemDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(EquipAlarmItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.EquipName == model.EquipName && s.Content == model.Content))
        {
            Oops.ThrowBah(ErrCode.E4003);
        }

        if (await _thisRepository.IsAnyAsync(s => s.EquipName == model.EquipName && s.Pos == model.Pos))
        {
            Oops.ThrowBah(ErrCode.E4006);
        }

        _queryService.Clear(); // 清空缓存
        return await _thisRepository.InsertAsync(model.Adapt<EquipAlarmItem>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(EquipAlarmItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.EquipName == model.EquipName && s.Content == model.Content && s.Id != model.Id))
        {
            Oops.ThrowBah(ErrCode.E4003);
        }

        if (await _thisRepository.IsAnyAsync(s => s.EquipName == model.EquipName && s.Pos == model.Pos && s.Id != model.Id))
        {
            Oops.ThrowBah(ErrCode.E4006);
        }

        _queryService.Clear(); // 清空缓存
        return await _thisRepository.UpdateAsync(model.Adapt<EquipAlarmItem>());
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody] List<long> ids)
    {
        _queryService.Clear(); // 清空缓存
        return await _thisRepository.DeleteAsync(m => ids.Contains(m.Id));
    }
}
